package app.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

import app.Manager;

public class Dialog extends JDialog implements ActionListener, IMyListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2531898529873181451L;

	private IntegerTextField areaTextField;
	private javax.swing.JButton closeButton;
	private javax.swing.JLabel headerLabel;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel pathTargetLabel;
	private javax.swing.JLabel pathXYZLabel;
	private IntegerTextField rowTextField;
	private javax.swing.JButton startButton;
	private javax.swing.JLabel stillToBeDoneLabel;
	private javax.swing.JButton xyzButton;

	private boolean gotXYZ = false;

	int row = 1;
	int stripe = 32;

	private JFileChooser xyzFileChooser;
	private JFileChooser txtFileChooser;

	public Dialog(int width, int height) {

		setSize(width, height);
		setLayout(new BorderLayout());
		setLocationRelativeTo(null);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);

		initComponents();
		createFileFilter();
		Manager.getInstance().addListener(this);
	}

	private void initComponents() {
		headerLabel = new javax.swing.JLabel();
		xyzButton = new javax.swing.JButton();
		pathXYZLabel = new javax.swing.JLabel();
		jLabel1 = new javax.swing.JLabel();
		rowTextField = new IntegerTextField(false);
		jLabel2 = new javax.swing.JLabel();
		startButton = new javax.swing.JButton();
		pathTargetLabel = new javax.swing.JLabel();
		stillToBeDoneLabel = new javax.swing.JLabel();
		closeButton = new javax.swing.JButton();
		areaTextField = new IntegerTextField(false);
		jLabel3 = new javax.swing.JLabel();

		areaTextField.setText("32");

		jLabel3.setText("° Streifen");

		headerLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
		headerLabel.setText("ETRS89 -> DHHN92");

		xyzButton.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
		xyzButton.setText("xyz- waehlen");
		xyzButton.setToolTipText("Punktwolke waehlen!");
		xyzButton.addActionListener(this);

		pathXYZLabel.setText("Pfad");

		jLabel1.setText("Jeden");

		rowTextField.setText("1");

		jLabel2.setText("Datensatz bearbeiten!");

		startButton.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
		startButton.setText("Start");
		startButton.setToolTipText("Startet die Transformation");
		startButton.addActionListener(this);
		startButton.setEnabled(false);

		pathTargetLabel.setText("Pfad");

		stillToBeDoneLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
		stillToBeDoneLabel.setText("test");

		closeButton.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
		closeButton.setText("Beenden");
		closeButton.addActionListener(this);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		xyzButton)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		pathXYZLabel))
												.addComponent(headerLabel)
												.addComponent(
														stillToBeDoneLabel)
												.addComponent(closeButton)
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.TRAILING,
																				false)
																				.addComponent(
																						areaTextField,
																						javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						jLabel1,
																						javax.swing.GroupLayout.Alignment.LEADING,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE))
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addComponent(
																										rowTextField,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										40,
																										javax.swing.GroupLayout.PREFERRED_SIZE)
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																								.addComponent(
																										jLabel2))
																				.addComponent(
																						jLabel3)))
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		startButton)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		pathTargetLabel)))
								.addContainerGap(303, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(headerLabel)
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(xyzButton)
												.addComponent(pathXYZLabel))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel1)
												.addComponent(
														rowTextField,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel2))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														areaTextField,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel3))
								.addGap(18, 18, 18)
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(startButton)
												.addComponent(pathTargetLabel))
								.addGap(18, 18, 18)
								.addComponent(stillToBeDoneLabel)
								.addGap(18, 18, 18).addComponent(closeButton)
								.addContainerGap(23, Short.MAX_VALUE)));

		pack();

	}

	private void createFileFilter() {
		xyzFileChooser = new JFileChooser("resource");
		xyzFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		xyzFileChooser.setDialogTitle("PointCloud-Auswaehlen");
		FileFilter xyz = new FileFilter() {

			@Override
			public String getDescription() {
				return "XYZ- Dateien(*.xyz)";
			}

			@Override
			public boolean accept(File f) {
				return f.getName().toLowerCase().endsWith("xyz")
						|| f.isDirectory();
			}
		};
		xyzFileChooser.setFileFilter(xyz);

		txtFileChooser = new JFileChooser("resource");
		txtFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		txtFileChooser.setDialogTitle("Geoid-Auswaehlen");
		FileFilter txt = new FileFilter() {

			@Override
			public String getDescription() {
				return "Projekt- Dateien(*.txt)";
			}

			@Override
			public boolean accept(File f) {
				return f.getName().toLowerCase().endsWith("txt")
						|| f.isDirectory();
			}
		};
		txtFileChooser.setFileFilter(txt);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == closeButton) {
			closeApplication();
		} else if (e.getSource() == xyzButton) {
			loadPointCloudFile();
		} else if (e.getSource() == startButton) {
			if (gotXYZ && rowDataIsValid() && stripeDataIsValid()) {
				row = Integer.valueOf(rowTextField.getText());
				Manager.getInstance().setStripe(
						Integer.valueOf(areaTextField.getText()));
				double time1 = System.currentTimeMillis();
				Manager.getInstance().startTransformation(row);
				double time2 = System.currentTimeMillis();
				double dif = (time2 - time1) / 1000.0;
				stillToBeDoneLabel.setText(Manager.getInstance().getCloudSize()
						+ " Datensaetze, - " + dif + "sec");
				pathTargetLabel.setText(Manager.getInstance()
						.getTargetFilePath());
			}
		}

	}

	private boolean stripeDataIsValid() {
		if (areaTextField.getText().isEmpty()) {
			JOptionPane.showMessageDialog(null,
					"Bitte Wert fuer Meridiansteifen eingeben!");
			return false;
		} else if (Integer.valueOf(areaTextField.getText()) < 25
				|| Integer.valueOf(areaTextField.getText()) > 40) {
			JOptionPane.showMessageDialog(null,
					"Bitte Eingabe fuer Meridiansteifen kontrollieren!");
			return false;
		}
		return true;
	}

	private boolean rowDataIsValid() {
		if (rowTextField.getText().isEmpty()) {
			JOptionPane.showMessageDialog(null, "Bitte Reihenwert eingeben!");
			return false;
		} else if (Integer.valueOf(rowTextField.getText()) == 0) {
			JOptionPane.showMessageDialog(null, "Bitte Zahl >0 eingeben!");
			return false;
		} else if (Integer.valueOf(rowTextField.getText()) > 100) {
			int ret = JOptionPane.showConfirmDialog(null, "Ist der Wert "
					+ Integer.valueOf(rowTextField.getText()) + " korrekt?",
					"Bestaetigung!", JOptionPane.YES_NO_OPTION);
			if (ret == JOptionPane.YES_OPTION) {
				return true;
			} else {
				return false;
			}
		}
		return true;
	}

	private void loadPointCloudFile() {
		int n = xyzFileChooser.showOpenDialog(this);
		if (n == JFileChooser.APPROVE_OPTION) {
			File file = xyzFileChooser.getSelectedFile();
			Manager.getInstance().setLoadedCloudFilePath(file.getPath());
			pathXYZLabel.setText(file.getAbsolutePath());
			gotXYZ = true;
		}
		checkForFiles();
	}

	private void checkForFiles() {
		if (gotXYZ) {
			startButton.setEnabled(true);
		}
	}

	private void closeApplication() {
		int ret = JOptionPane.showConfirmDialog(null, "Wollen Sie beenden?",
				"Programm Beenden!", JOptionPane.YES_NO_OPTION);
		if (ret == JOptionPane.YES_OPTION) {
			System.exit(0);
		}
	}

	@Override
	public void progressChanged(String string) {
		stillToBeDoneLabel.setText(string);
		
		System.out.println(string);
	}

}
